package com.atguigu.boot.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MessageListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "queue.boot.230301", durable = "true", autoDelete = "false"),
            exchange = @Exchange(name = "exchange.boot.test.230301", durable = "true", autoDelete = "false"),
            key = {"routing.key.boot.test.230301"}
    ))
    public void processMessage(String messageContent, Message message, Channel channel) throws IOException {
        try {
            System.out.println("messageContent = " + messageContent);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            throw new RuntimeException(e);
        }
    }

}
